package com.hd.trans.files.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import java.util.LinkedHashSet;
import java.util.List;

abstract public class BaseAdapter<M, H extends RecyclerView.ViewHolder> extends ListAdapter<M, H> {


    private final LinkedHashSet<Integer> mChildClickViewIds = new LinkedHashSet<>();

    private OnItemClickListener<M> mOnItemClickListener;
    private OnItemChildClickListener<M> mOnItemChildClickListener;

    public BaseAdapter(@NonNull DiffUtil.ItemCallback<M> diffCallback) {
        super(diffCallback);
    }

    protected BaseAdapter(@NonNull AsyncDifferConfig<M> config) {
        super(config);
    }

    @NonNull
    @Override
    public H onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(), parent, false);
        H viewHolder = createViewHolder(itemView);
        viewHolder.itemView.setOnClickListener(view -> {
            if (mOnItemClickListener != null) {
                int position = viewHolder.getBindingAdapterPosition();
                mOnItemClickListener.onItemClick(viewHolder.itemView.getId(), (M) getItem(position), position);
            }
        });
        if (mOnItemChildClickListener != null) {
            for (int id : mChildClickViewIds) {
                View childView = viewHolder.itemView.findViewById(id);
                if (childView != null) {
                    childView.setOnClickListener(view -> {
                        int position = viewHolder.getBindingAdapterPosition();
                        mOnItemChildClickListener.onItemChildClick(childView, (M) getItem(position), position);
                    });
                }
            }
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull H holder, int position) {
        bindViewHolder(holder, getItem(position));
    }

    @Override
    public void onBindViewHolder(@NonNull H holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            bindViewHolder(holder, getItem(position), payloads);
        }
    }

    protected void bindViewHolder(H holder, M m, @NonNull List<Object> payloads) {
    }

    public void setOnItemClickListener(OnItemClickListener<M> listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener<M> listener) {
        this.mOnItemChildClickListener = listener;
    }

    protected void setOnItemChildClick(View view, int position) {
        if (mOnItemChildClickListener != null) {
//            mOnItemChildClickListener.onItemChildClick(view, position, position);
        }
    }

    public void addChildClickViewIds(@IdRes int... viewIds) {
        for (int id : viewIds) {
            mChildClickViewIds.add(id);
        }
    }

    abstract void bindViewHolder(H holder, M m);

    abstract H createViewHolder(View itemView);

    abstract int getLayoutId();

    public interface OnItemClickListener<M> {
        void onItemClick(@IdRes int viewId, M m, int position);
    }

    public interface OnItemChildClickListener<M> {
        void onItemChildClick(@NonNull View view, M m, int position);
    }
}
